﻿using System;
//using System.Web.Mvc;
using System.Collections.Generic;
using EPiServer.Core;
using EPiServer.Framework.Localization;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;

namespace OpenWaves.EPiServer.LongStringLimitedProperty
{
    [EditorDescriptorRegistration(TargetType = typeof(string), UIHint = PropertyLongStringLimited.UIHint)]
    public class PropertyLongStringLimitedDescriptor : EditorDescriptor
    {
        public PropertyLongStringLimitedDescriptor()
        {
            this.ClientEditingClass = "ow.epi.longStringProperty.widgets.LongStringLimited";
        }

        private string GetTranslationValue(string resourceName)
        {
            return
                LocalizationService.Current.GetString(String.Format("/{0}/{1}", this.PropertyUniqueName, resourceName), FallbackBehaviors.MissingMessage);
        }

        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            var propertyModel = metadata.Model as PropertyLongString;
            var propertyValue = (propertyModel != null && !propertyModel.IsNull) ? propertyModel.Value: string.Empty;

 	        base.ModifyMetadata(metadata, attributes);
            metadata.EditorConfiguration.Add("currentContent", propertyValue);
            metadata.EditorConfiguration.Add("maxContentLengthCounterText", GetTranslationValue("maxContentLengthCounterText"));
        }

        protected string PropertyUniqueName
        {
            get { return PropertyLongStringLimited.UniqueName; }
        }
    }
}